<template>
    <view class="memberVip">
        <view class="bg">
            <view class="header">
                <view class="picTxt acea-row row-middle">
                    <view class="pictrue">
                        <image :src="userInfo.avatar"></image>
                    </view>
                    <view class="text acea-row row-middle">
                        <view class="name line1">{{userInfo.nickname}}</view>
                        <view class="vip" v-if='userInfo.vip'>
                            <image :src="userInfo.vipIcon"></image>
                            {{userInfo.vipName}}
                        </view>
                    </view>
                </view>
            </view>
            <view class="experience">
                <view class="title">当前会员</view>
                <!-- <view class="num">{{levelInfo}}</view> -->
                <view class="axis">
                    <!-- <view class="bar">
                        <view class="barCon">
                            <view class="solidBar" :style="'width: ' + widthLen +'%;'"></view>
                        </view>
                        <view class="acea-row row-around row-middle">
                            <view class="spotw acea-row row-center" v-for="(item,index) in levelList" :key='index'>
                                <view class="spot"
                                    :class="current >item.experience?'past':'' + ' ' + current==item.experience?'on':''">
                                </view>
                            </view>
                        </view>
                    </view> -->
                    <view class="numList acea-row row-around row-middle">
                        <!-- 	<view class="item" :class="current >=item.experience?'past':''"
                            v-for="(item,index) in levelList" :key="index">{{item.experience}}</view> -->
                    </view>
                </view>
                <view class="vipList acea-row">
                    <view class="item">
                        <view class="pictrue">
                            <image src="./../static/vip01.png"></image>
                        </view>
                        <view class="name">会员折扣</view>
                    </view>
                    <view class="item">
                        <view class="pictrue">
                            <image src="./../static/vip02.png"></image>
                        </view>
                        <view class="name">专属徽章</view>
                    </view>
                    <view class="item">
                        <view class="pictrue">
                            <image src="./../static/vip03.png"></image>
                        </view>
                        <view class="name">会员升级</view>
                    </view>
                    <!-- <view class="item">
                        <view class="pictrue">
                            <image src="./../static/vip04.png"></image>
                        </view>
                        <view class="name">经验积累</view>
                    </view>
                    <view class="item">
                        <view class="pictrue">
                            <image src="./../static/vip05.png"></image>
                        </view>
                        <view class="name">更多特权</view>
                    </view> -->
                </view>
            </view>
            <view class="module">
                <view class="public_title acea-row row-middle">
                    <view class="icons"></view>
                    充值类型
                </view>
                <view class="gainList">
                    <view class="gainListes">

                        <view v-for="(item,index) in picList" :class="ids==index?'gainListes2':'gainListes1'"
                              @click="picCharge(index,item)">
                            <span>{{item.buyDay}}天</span>
                            <span>{{item.price}}元</span>
                        </view>

                    </view>

                </view>
            </view>
            <view class="bottomes" v-if="isshowes">
                <span>{{price1}}元</span>
                <view class="shop">
                    <form @submit="submitSub" report-submit='true'>
                        <button style="background: linear-gradient(90deg, #FF7931 0%, #F11B09 100%);color: #ffffff;"
                                formType="submit">购买
                        </button>
                    </form>
                    <!-- 购买 -->
                </view>
            </view>
        </view>
        <!-- 	<view class="detailed" v-if="expList.length">
            <view class="public_title acea-row row-middle">
                <view class="icons"></view>经验值明细
            </view>
            <view class="list">
                <view class="item acea-row row-between-wrapper" v-for="(item,index) in expList" :key="index">
                    <view class="text">
                        <view class="name">{{item.title}}</view>
                        <view class="data">{{item.add_time}}</view>
                    </view>
                    <view class="num" v-if="item.pm">+{{item.number}}</view>
                    <view class="num on" v-else>-{{item.number}}</view>
                </view>
            </view>
        </view> -->
        <!-- <view class='loadingicon acea-row row-center-wrapper' v-if="expList.length">
            <text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
        </view> -->
        <!-- <home></home> -->
        <!-- #ifdef MP -->
        <!-- <authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize> -->
        <!-- #endif -->
        <view>
            <payment :payMode='payMode' :pay_close="pay_close" @onChangeFun='onChangeFun' :order_id="pay_order_id"
                     :totalPrice='totalPrice'></payment>
        </view>
        <view>
            <form @submit="submitSub" report-submit='true'>
                <view class="payment">
                    <button class='but' formType="submit"> {{active ? '立即转入': '立即充值' }}</button>
                </view>
            </form>
        </view>
    </view>

</template>

<script>

	import {
		rechargeRoutine,
		rechargeWechat,
		getRechargeApi,
		transferIn,
		appWechat
	} from '@/api/user.js';
	import { wechatQueryPayResult } from '@/api/order.js';
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from "vuex";
	// #ifdef MP
	import authorize from '@/components/Authorize';
	// #endif
	
	export default {
		components: {
			// #ifdef MP
			authorize,
			// #endif
			
		},
		data() {
			let that = this;
			return {
				buyDay:"",
				pay_order_id: "",
				levelInfo: '',
				isshowes: true,
				levelList: [],
				
				current: 0,
				widthLen: 0,
				loading: false,
				pay_close: false,
				loadend: false,
				loadTitle: '加载更多', //提示语
				page: 1,
				limit: 20,
				price:"",
				price1:"",
				ids:"",
				num: "",
				expList: [],
				now_money: 0,
				navRecharge: ['账户充值', '佣金转入'],
				active: 0,
				number: '',
				placeholder: "0.00",
				from: '',
				isAuto: false, //没有授权的不会自动授权
				isShowAuth: false, //是否隐藏授权
				picList: [],
				activePic: 0,
				money: "",
				numberPic: '',
				rechar_id: 0,
				rechargeAttention: [],
				ids:100,
			};
		},
		computed: mapGetters(['isLogin', 'systemPlatform','userInfo']),
		watch:{
			isLogin:{
				handler:function(newV,oldV){
					if(newV){
						this.getRecharge();
					}
				},
				deep:true
			}
		},
		onLoad(options) {
			
			// #ifdef H5
			this.from = this.$wechat.isWeixin() ? "public" : "weixinh5";
			// #endif
			if (this.isLogin) {
				this.getRecharge();
			} else {
				toLogin();
			}
		},
		methods: {
			clickprice(price,index) {
				
				// this.ids = index,
				console.log(price)
					this.money = price
			},
		
			
			getInfo: function() {
				let that = this;
				getlevelInfo().then(res => {
					let levelList = res.data;
					let list = []
					that.levelList = levelList;
					levelList.map((item, index) => {
						if (item.experience <= that.levelInfo) {
							list.push(item.experience)
						}
					})
					let maxn = Math.max.apply(null, list);
					that.current = maxn;
					// 解决len取的值没有时；
					let levelListLen = levelList[list.length] ? levelList[list.length] : levelList[list
						.length - 1];
					// 解决除数不能为0
					let divisor = levelListLen.experience - maxn ? levelListLen.experience - maxn : 1;
					// 每小格所占的百分比
					let per = (that.levelInfo - maxn) / divisor / levelList.length;
			
					that.widthLen = ((list.length - 0.5) / (levelList.length)) * 100 + per * 100
			
				}).catch(function(res) {
					return that.$util.Tips({
						title: res
					});
				})
			},
			getlevelList: function() {
				let that = this
				if (this.loadend) return false;
				if (this.loading) return false;
				getlevelExpList({
					page: that.page,
					limit: that.limit
				}).then(res => {
					let list = res.data.list,
						loadend = list.length < that.limit;
					let expList = that.$util.SplitArray(list, that.expList);
					that.$set(that, 'expList', expList);
					that.loadend = loadend;
					that.loadTitle = loadend ? '我也是有底线的' : '加载更多';
					that.page = that.page + 1;
					that.loading = false;
				}).catch(err => {
					that.loading = false;
					that.loadTitle = '加载更多';
				});
			},
			
			picCharge(idx, item) {
				this.ids = idx,
				console.log(item)
				this.price1 = item.price
				this.buyDay=item.buyDay
				this.activePic = idx;
				if (item === undefined) {
					this.rechar_id = 0;
					this.numberPic = "";
				} else {
					this.money = "";
					this.rechar_id = item.id;
					this.numberPic = item.price;
				}
			},
			getRecharge() {
				getRechargeApi()
					.then(res => {
						console.log(res)
						this.picList = res.data.rechargeQuota;
						if (this.picList[0]) {
							this.rechar_id = this.picList[0].id;
							this.numberPic = this.picList[0].price;
						}
						this.rechargeAttention = res.data.rechargeAttention || [];
					})
					.catch(res => {
						this.$dialog.toast({
							mes: res
						});
					});
			},
			onLoadFun: function() {
				this.getRecharge();
			},
			// 授权关闭
			authColse: function(e) {
				this.isShowAuth = e
			},
			navRecharges: function(index) {
				this.active = index;
			},
			back(){
				var detle=1
				uni.navigateBack({
				  detle:detle
				});
			},
			/*
			 * 用户充值
			 */
			submitSub: function(e) {
				let that = this
				let value = e.detail.value.number;
				// 转入余额
				if (that.active) {
					if (parseFloat(value) < 0 || parseFloat(value) == NaN || value == undefined || value == "") {
						return that.$util.Tips({
							title: '请输入金额'
						});
					}
					uni.showModal({
						title: '转入余额',
						content: '转入余额后无法再次转出，确认是否转入余额',
						success(res) {
							if (res.confirm) {
								transferIn({
											price: parseFloat(value)
								}).then(res => {
									that.$store.commit("changInfo", {
										amount1: 'brokeragePrice',
										amount2: that.$util.$h.Sub(that.userInfo.brokeragePrice, parseFloat(value))
									});
									return that.$util.Tips({
										title: '转入成功',
										icon: 'success'
									}, {
										tab: 5,
										// url: '/pages/users/user_money/index'
										
									},
								    
									
									);
								}).catch(err=>{
									return that.$util.Tips({
										title: err
									});
								})
							} else if (res.cancel) {
								return that.$util.Tips({
									title: '已取消'
								});
							}
						},
					})
				} else {
					uni.showLoading({
						title: '正在支付',
					})
				   
					let money = parseFloat(this.money);
					
					if (this.rechar_id == 0) {
						if (Number.isNaN(money)) {
							return that.$util.Tips({
								title: '充值金额必须为数字'
							});
						}
						if (money <= 0) {
							return that.$util.Tips({
								title: '充值金额不能为0'
							});
						}
						if (money > 50000) {
							return that.$util.Tips({
								title: '充值金额最大值为50000'
							});
						}
					} else {
						money = this.numberPic
						
						 console.log("签署",this.numberPic)
					}
					// #ifdef MP
					rechargeRoutine({
						price: money,
						payType: "weixin",
						rechar_id: that.rechar_id,
						vipType:"dbvip",
						userId:this.$store.state.app.uid,
						payVipDay:this.buyDay
					}).then(res => {
						console.log("支付策划经过",res)
						uni.hideLoading();
						let jsConfig = res.data.data.jsConfig;
						
						uni.requestPayment({
							
							timeStamp: jsConfig.timeStamp,
							nonceStr: jsConfig.nonceStr,
							package: jsConfig.packages,
							signType: jsConfig.signType,
							paySign: jsConfig.paySign,
							success: function(res) {
								console.log("支付策划经过",res)
								console.log(1231312,"支付")
								uni.navigateBack(1)
								that.$store.commit("changInfo", {
									amount1: 'nowMoney',
									// amount2: that.$util.$h.Add(value, that.userinfo.nowMoney)
								});
								
								//that.$set(that, 'userinfo.nowMoney', that.$util.$h.Add(value, that.userinfo.nowMoney));
								return that.$util.Tips({
									title: '支付成功',
									icon: 'success',
									
								}, {
									tab: 5,
									// url: '/pages/users/user_money/index'
								},
								
								console.log(2)
								);
								
								console.log(123,"支付成功123131312312313")
								// var title=1
								// uni.navigateBack({
								// 	title:title
								// })
								that.back()
								console.log(12222)
							},
							fail: function(err) {
								return that.$util.Tips({
									title: '支付失败'
								});
							},
							complete: function(res) {
								if (res.errMsg == 'requestPayment:cancel') return that.$util.Tips({
									title: '取消支付'
								});
							}
						})
					}).catch(err => {
						uni.hideLoading();
						return that.$util.Tips({
							title: err
						})
					});
					// #endif
					// #ifdef H5 
					rechargeWechat({
						price: money,
						from: that.from,
						rechar_id: that.rechar_id,
						vipType:"dbvip",
						payVipDay:this.buyDay,
						userId:this.$store.state.app.uid,
						payType: "weixin"
					}).then(res => {
					console.log("支付策划经过",res)
						let jsConfig = res.data.jsConfig;
						let orderNo = res.data.orderNo;
						let data = {
							timestamp:jsConfig.timeStamp,
							nonceStr:jsConfig.nonceStr,
							package:jsConfig.packages,
							signType:jsConfig.signType,
							paySign:jsConfig.paySign
						};
						if (that.from == "weixinh5") {
							let domain = encodeURIComponent(location.href.split('/pages')[0]);
							let urls = jsConfig.mwebUrl + '&redirect_url='+ domain + '/pages/users/user_money/index';
							location.replace(urls);
							return that.$util.Tips({
								tab: 5,
								// url: '/pages/users/user_money/index'
							}
						
							);
							return that.$util.Tips({
								title: '支付成功',
								icon: 'success'
							}, {
								tab: 5,
								// url: '/pages/users/user_money/index'
							},
							console.log(4)
							);
							
						} else {
							that.$wechat.pay(data)
								.finally(() => {
									that.$store.commit("changInfo", {
										amount1: 'nowMoney',
										// amount2: that.$util.$h.Add(value, that.userinfo.nowMoney)
									});
									// that.$set(that, 'userinfo.nowMoney', that.$util.$h.Add(value, that.userinfo.nowMoney));
									wechatQueryPayResult(orderNo).then(res => {
										console.log("支付成功111")
										return that.$util.Tips({
											title: '支付成功',
											icon: 'success'
										}, {
											tab: 5,
											// url: '/pages/users/user_money/index'
										},
										
										console.log("支付成功22222")
										);
										console.log("支付成功2")
									}).cache(err => {
										return that.$util.Tips({
											title: err
										});
									})
								})
								.catch(function(err) {
									return that.$util.Tips({
										title: '支付失败'
									});
								});
						}
					}).catch(res=>{
						uni.hideLoading();
						return that.$util.Tips({
							title: res
						});
					})
					// #endif
				}
			},
		
		  pay_complete: function() {
			  console.log("成功回调111111111111111111")
		  	// this.status = false;
		  	// this.page = 1;
		  	// this.$set(this, 'bargain', []);
		  	// this.$set(this, 'pay_close', false);
		  	// this.getBargainUserList();
		  },
		  /**
		   * 支付失败回调
		   * 
		   */
		  pay_fail: function() {
		  	this.pay_close = false;
		  },
		},
		onReachBottom: function() {
			this.getlevelList();
		}
	}
</script>

<style lang="scss">
.memberVip {
		.bg {
			background-color: #fff;

			.header {
				background-image: url('');
				background-repeat: no-repeat;
				background-size: 100% 100%;
				width: 100%;
				height: 380rpx;
				background-color: #fff;

				.picTxt {
					padding: 28rpx 39rpx;

					.pictrue {
						width: 70rpx;
						height: 70rpx;
						border-radius: 50%;

						image {
							width: 100%;
							height: 100%;
							border-radius: 50%;
						}
					}

					.text {
						color: #FAE2C1;
						margin-left: 23rpx;

						.name {
							max-width: 385rpx;
						}

						.vip {
							padding: 6rpx 18rpx;
							border: 1px solid rgba(250, 226, 193, 1);
							border-radius: 20rpx;
							font-size: 18rpx;
							margin-left: 15rpx;
							display: flex;
							align-items: center;

							image {
								width: 20rpx;
								height: 20rpx;
								border-radius: 50%;
								margin-right: 8rpx;
							}
						}
					}
				}
			}

			.experience {
				background-image: url('');
				background-repeat: no-repeat;
				background-size: 100% 100%;
				width: 690rpx;
				height: 260rpx;
				margin: -250rpx auto 0 auto;
				border-radius: 23rpx;
				padding: 22rpx 27rpx;
				box-sizing: border-box;

				.title {
					font-size: 24rpx;
					color: #AE8B4A;
				}

				.num {
					font-size: 60rpx;
					color: #775C29;
					margin-top: 6rpx;
				}

				.axis {
					margin: 10rpx 0 15rpx 0;
					overflow: hidden;

					.bar {
						width: 630rpx;

						.spotw {
							width: 96rpx;
						}

						.barCon {
							width: 100%;
							height: 3rpx;
							background: #D7BD89;
							border-radius: 2rpx;

							.solidBar {
								width: 0;
								height: 100%;
								background: #775C29;
								border-radius: 2rpx;
								transition: width 0.6s ease;
							}
						}

						.spot {
							width: 8rpx;
							height: 8rpx;
							background: #D7BD89;
							border-radius: 50%;
							margin-top: -5rpx;

							&.past {
								background: #775C29;
							}

							&.on {
								background: #775C29;
								box-shadow: 0rpx 0rpx 8rpx #000;
							}
						}
					}

					.numList {
						font-size: 26rpx;
						color: #D7BD89;
						margin-top: 14rpx;

						.item {
							width: 96rpx;
							text-align: center;

							&.past {
								color: #775C29;
							}
						}
					}
				}

				.vipList {
					display: flex;
					justify-content: space-evenly;

					.item {
						width: 20%;
						text-align: center;

						.pictrue {
							width: 70rpx;
							height: 70rpx;
							margin: 0 auto;

							image {
								width: 100%;
								height: 100%;
							}
						}

						.name {
							font-size: 24rpx;
							color: #403D4E;
							margin-top: 18rpx;
						}
					}
				}
			}


			.bottomes {
				width: 100%;
				display: flex;
				align-items: center;
				justify-content: space-around;
				padding: 20upx 20upx;
				border: 1px solid #eee;
				border-radius: 20upx;
				// margin: 0 auto;
				box-shadow: 0 0 20upx #eee;
				margin-top: 30upx;
				position: fixed;
				bottom: 0;
				left: 0;
				z-index: 999;
				background: #ffffff;

				.shop {
					width: 200upx;
					background: linear-gradient(90deg, #FF7931 0%, #F11B09 100%);
					color: #ffffff;
					text-align: center;
					padding: 25upx 20upx;
					border-radius: 20upx;
				}
				span{
					font-size: 18px;
					color: greenyellow;
				}
			}

			.module {
				padding: 40rpx 30rpx 30upx 30rpx;
				margin-bottom: 160upx;

				.gainList {
					margin-top: 10rpx;

					.gainListes {
						margin: 0 auto;

						.gainListes1 {
							width: 92%;

							display: flex;
							justify-content: space-between;
							border: 1px solid #eee;
							border-radius: 20upx;
							padding: 30upx 50upx;
							margin: 0 auto;
							margin-top: 40upx;
						}

						.gainListes2 {
							width: 92%;

							display: flex;
							justify-content: space-between;
							// border: 1px solid #eee;
							border-radius: 20upx;
							padding: 30upx 50upx;
							margin: 0 auto;
							margin-top: 40upx;
							border: 1px solid greenyellow;
							font-size: 18px;
						}
					}

					.item {
						height: 130rpx;
						position: relative;

						.picTxt {
							.pictrue {
								width: 70rpx;
								height: 70rpx;
								border-radius: 50%;
								background: linear-gradient(-45deg, rgba(249, 119, 107, 1) 0%, rgba(255, 147, 137, 1) 100%);
								text-align: center;
								line-height: 70rpx;
								color: #fff;

								&.on {
									background: linear-gradient(-45deg, rgba(254, 160, 96, 1) 0%, rgba(255, 204, 170, 1) 100%);
								}

								&.on2 {
									background: linear-gradient(-45deg, rgba(157, 208, 116, 1) 0%, rgba(161, 214, 124, 1) 100%);
								}
							}

							.text {
								margin-left: 30rpx;
								width: 400rpx;

								.name {
									font-size: 30rpx;
									color: #282828;
								}

								.info {
									font-size: 24rpx;
									color: #999999;
									margin-top: 6rpx;
								}
							}
						}

						.button {
							width: 140rpx;
							height: 50rpx;
							background: linear-gradient(-90deg, rgba(231, 182, 103, 1) 0%, rgba(255, 234, 181, 1) 100%);
							border-radius: 25rpx;
							text-align: center;
							line-height: 50rpx;
							font-size: 26rpx;
							color: #8D5306;
						}

						&~.item {
							&::after {
								position: absolute;
								content: ' ';
								width: 720rpx;
								height: 1rpx;
								background: rgba(245, 245, 245, 1);
								top: 0;
								left: 0;
							}
						}
					}
				}
			}
		}

		.public_title {
			color: #282828;
			font-size: 30rpx;

			.icons {
				width: 6rpx;
				height: 28rpx;
				background: rgba(230, 192, 131, 1);
				margin-right: 10rpx;
			}
		}

		.detailed {
			padding: 30rpx 30rpx 0 30rpx;
			margin-top: 15rpx;
			background-color: #fff;

			.list {
				margin-top: 15rpx;

				.item {
					height: 122rpx;
					border-bottom: 1px solid #EEEEEE;

					.text {
						.name {
							font-size: 28rpx;
							color: #282828;
						}

						.data {
							color: #999;
							font-size: 24rpx;
						}
					}

					.num {
						font-size: 32rpx;
						color: $theme-color;
					}

					.on {
						color: #16AC57;
					}
				}
			}
		}
	}
</style>

